package org.example.group5back.controller;

import org.example.group5back.entity.BaseResponse;
import org.example.group5back.entity.PersonEntity;
import org.example.group5back.entity.ResultBody;
import org.example.group5back.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


@RestController
public class PersonController {

    @Autowired
    PersonService PersonService;

    @RequestMapping("/savePerson")
    @ResponseBody
    @CrossOrigin(origins = "*",allowedHeaders = "*")
    public ResultBody savaPerson(PersonEntity ps){
        System.out.println(ps.toString());
        if(PersonService.savePerson(ps)){
            return new ResultBody(0,"success");
        }else{
            return new ResultBody(1,"fail");
        }
    }

    @RequestMapping("/deletePerson")
    @CrossOrigin(origins = "*",allowedHeaders = "*")
    public ResultBody deletePerson(int id){
        if(PersonService.deletePerson(id)){
            return new ResultBody(0,"success");
        }else{
            return new ResultBody(1,"fail");
        }
    }

    @RequestMapping("/listPerson")
    @CrossOrigin(origins = "*", allowedHeaders = "*")
    public BaseResponse<PersonEntity> listPerson(){
        List<PersonEntity> personEntityList = PersonService.finAllPerson();
        return new BaseResponse<PersonEntity>(0,"ok",
                personEntityList.size(),personEntityList);
    }


    @RequestMapping("/selectPerson")
    @CrossOrigin(origins = "*", allowedHeaders = "*")
    public BaseResponse<PersonEntity>selectPerson(@RequestParam(value="page",required=false) Integer page,
                                                        @RequestParam(value = "limit",required = false) Integer limit){
        List<PersonEntity> list = PersonService.finAllPerson();
        Integer pageNum = (page - 1) * limit;
        List<PersonEntity> show_list = PersonService.showPageLimit(pageNum,limit);
        return new BaseResponse<PersonEntity>(0,"ok",
                list.size(),show_list);
    }
    @RequestMapping("/selectPersonByName")
    @CrossOrigin(origins = "*", allowedHeaders = "*")
    public BaseResponse<PersonEntity>selectPersonByName(@RequestParam(value="page",required=false) Integer page,
                                                  @RequestParam(value = "limit",required = false) Integer limit,
                                                  @RequestParam(value = "person_number",required = false) String person_number,
                                                  @RequestParam(value = "person_name",required = false) String person_name,
                                                  @RequestParam(value = "degree",required = false) String degree,
                                                  @RequestParam(value = "person_position",required = false) String person_position,
                                                  @RequestParam(value = "telephone",required = false) String telephone){
        System.out.println(page);
        System.out.println(limit);
        if(person_number.isEmpty()){
            System.out.println("person_number is null");
            person_number = null;
        }
        if(person_name.isEmpty()){
            System.out.println("person_name is null");
            person_name = null;
        }
        if(degree.isEmpty()){
            System.out.println("degree is null");
            degree = null;
        }
        if(person_position.isEmpty()){
            System.out.println("person_position is null");
            person_position = null;
        }
        if(telephone.isEmpty()){
            System.out.println("telephone is null");
            telephone = null;
        }

        List<PersonEntity> list = PersonService.selectPersonByName(person_number,person_name,degree,person_position,telephone);

        List<PersonEntity> show_list = selectPersonByNameMySelf(list,page,limit);

        return new BaseResponse<PersonEntity>(0,"ok",
                list.size(),show_list);
    }
    public static List<PersonEntity> selectPersonByNameMySelf(List<PersonEntity> list,Integer page,Integer limit){
        List<PersonEntity> show_list = new ArrayList<PersonEntity>();
        if(page * limit > list.size()){
            show_list = list.subList((page-1)*limit,list.size());
        }else{
            show_list = list.subList((page-1)*limit,page*limit);
        }
        return show_list;
    }
}